package com.piotrs10.bsk.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.piotrs10.bsk.model.Faktura;
import com.piotrs10.bsk.model.Samochod;

public class FakturaDao extends BaseDao {
	public Faktura get(String numer) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		Faktura faktura = null;
		try {
			statement = connection.prepareStatement("SELECT data_wystawienia, " +
					"sprzedaz_id FROM faktura WHERE numer like ?;");
			statement.setString(1, numer);
			ResultSet set  = statement.executeQuery();
			set.next();
			faktura = new Faktura(numer, set.getDate(1), set.getInt(2));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return faktura;
	}
	
	public void delete(String id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement("DELETE FROM Faktura " +
						"WHERE numer like ?");
			statement.setString(1, id);
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public ArrayList<Faktura> getAll() {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		ArrayList<Faktura> list = null;
		try {
			statement = connection.prepareStatement("SELECT numer, data_wystawienia," +
					"sprzedaz_id FROM faktura;");
			ResultSet set = statement.executeQuery();
			list = new ArrayList<Faktura>();
			while(set.next()) {
				list.add(new Faktura(set.getString(1), set.getDate(2), set.getInt(3)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	public void save(boolean insert, Faktura faktura) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			if(insert) {
				statement = connection.prepareStatement("INSERT INTO Faktura " +
						"(numer, data, sprzedaz_id) VALUES " +
						"(?, ?, ?)");
			} else {
				statement = connection.prepareStatement(
						"UPDATE Faktura SET numer = ? , data = ?, sprzedaz_id = ? " +
						"WHERE numer like ?");
			}
			statement.setString(1, faktura.getNumer());
			statement.setDate(2, faktura.getData());
			statement.setInt(3, faktura.getSprzedaz());

			if(!insert)
				statement.setString(4, faktura.getNumer());
			
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
